/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import java.io.IOException;
//import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "RequestInfo", urlPatterns = {"/RequestInfo"})
public class RequestInfo extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        response.setContentType("text/html;charset=UTF-8");
//        PrintWriter out = response.getWriter();
//        try {
//            /* TODO output your page here
//            out.println("<html>");
//            out.println("<head>");
//            out.println("<title>Servlet RequestInfo</title>");  
//            out.println("</head>");
//            out.println("<body>");
//            out.println("<h1>Servlet RequestInfo at " + request.getContextPath () + "</h1>");
//            out.println("</body>");
//            out.println("</html>");
//             */
//        } finally {            
//            out.close();
//        }
          String page;

    /**ApplicationResources provides a simple API 
     * for retrieving constants and other 
     * preconfigured values**/
//    ApplicationResources resource = 
//      ApplicationResources.getInstance();
//    try {
//
//      // Use a helper object to gather parameter 
//      // specific information.
//      RequestHelper helper = new
//         RequestHelper(request);
//
//      Command cmdHelper= helper.getCommand();
//
//      // Command helper perform custom operation
//      page = cmdHelper.execute(request, response);
//
//    }
//    catch (Exception e) {
//      LogManager.logMessage(
//        "EmployeeController:exception : " + 
//        e.getMessage());
//      request.setAttribute(resource.getMessageAttr(), 	
//        "Exception occurred : " + e.getMessage());
//      page = resource.getErrorPage(e);
//    }
//    // dispatch control to view
//    dispatch(request, response, page);          
           try {
           // Set the attribute and Forward to hello.jsp
           request.setAttribute ("servletName", "servletToJsp");
           getServletConfig().getServletContext().getRequestDispatcher(
                   "/WEB-INF/jsp/hello.jsp").forward(request, response);
       } catch (Exception ex) {
           //ex.printStackTrace ();
       }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//       response.setContentType("text/html");
//        PrintWriter out = response.getWriter();
//        out.println("<html>");
//        out.println("<body>");
//        out.println("<head>");
//        out.println("<title>Request Information Example</title>");
//        out.println("</head>");
//        out.println("<body>");
//        out.println("<h3>Request Information Example</h3>");
//        out.println("Method: " + request.getMethod());
//        out.println("Request URI: " + request.getRequestURI());
//        out.println("Protocol: " + request.getProtocol());
//        out.println("PathInfo: " + request.getPathInfo());
//        out.println("Remote Address: " + request.getRemoteAddr());
//        out.println("</body>");
//        out.println("</html>");
    processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

      protected void dispatch(HttpServletRequest request, 
    HttpServletResponse response,
    String page) 
  throws  javax.servlet.ServletException, 
    java.io.IOException {
    RequestDispatcher dispatcher = 
      getServletContext().getRequestDispatcher(page);
        dispatcher.forward(request, response);
  }
}
